package com.hecai.scanning.runner;

import com.hecai.scanning.config.ConfigProperties;
import com.hecai.scanning.thread.BillingRedemption;
import com.hecai.scanning.util.GreenJadorScanner;
import com.hecai.scanning.util.HttpUtil;
import com.hecai.scanning.websocket.MessageComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 当springboot启动之后，马上启动扫描枪线程，并打开默认浏览器，显示拍照界面
 *
 * @author VitalYang
 * @create 2018-12-10 09:17
 */
@Slf4j
@Component
public class ScannerRunner implements CommandLineRunner
{
    private static final String LOCAL_ELOAM_URL = "http://localhost:8449/index";

    @Autowired
    private ConfigProperties properties;

    @Autowired
    private GreenJadorScanner jadorScanner;

    @Autowired
    private BillingRedemption redemption;

    @Override
    public void run(String... args) throws Exception
    {
        // 自注册操作
        String hallNumber = properties.getHallNumber();
        String windowNumber = properties.getWindowNumber();
        Map param = new HashMap();
        param.put("hallNumber", hallNumber);
        param.put("windowNumber", windowNumber);
        String centerIpAddr = properties.getCenterIpAddr();
        String autoRegedit = properties.getAutoRegegit();
        if (HttpUtil.doPost(centerIpAddr + autoRegedit, param) == "false")
        {
            log.error("自注册客户端失败，接口：{}", centerIpAddr + autoRegedit);
            return;
        }
        log.info("自注册客户端成功，请点击开始刮奖，开始刮奖操作……");
        browse(LOCAL_ELOAM_URL);
        jadorScanner.scanComms();
        (new Thread(jadorScanner)).start();
        redemption.start();
    }

    private void browse(String url) throws Exception
    {
        // 获取操作系统的名字
        String osName = System.getProperty("os.name", "");
        if (osName.startsWith("Mac OS"))
        {
            // 苹果的打开方式
            Class fileMgr = Class.forName("com.apple.eio.FileManager");
            Method openURL = fileMgr.getDeclaredMethod("openURL",
                    new Class[]{String.class});
            openURL.invoke(null, new Object[]{url});
        } else if (osName.startsWith("Windows"))
        {
            // windows的打开方式。
            Runtime.getRuntime().exec(
                    "rundll32 url.dll,FileProtocolHandler " + url);
        } else
        {
            // Unix or Linux的打开方式
            String[] browsers = {"firefox", "opera", "konqueror", "epiphany",
                    "mozilla", "netscape"};
            String browser = null;
            for (int count = 0; count < browsers.length && browser == null; count++)
            // 执行代码，在brower有值后跳出，
            // 这里是如果进程创建成功了，==0是表示正常结束。
            {
                if (Runtime.getRuntime()
                        .exec(new String[]{"which", browsers[count]})
                        .waitFor() == 0)
                {
                    browser = browsers[count];
                }
            }
            if (browser == null)
            {
                throw new Exception("Could not find web browser");
            } else
            // 这个值在上面已经成功的得到了一个进程。
            {
                Runtime.getRuntime().exec(new String[]{browser, url});
            }
        }
    }
}
